﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.IO;
using WINBASE.Utils;

namespace WINBASE
{
    public partial class frmRmrb : WINBASE.WLCMainForm
    {
        int TrxNo;
        string MainTableName;
        DataTable dtRmrp1;

        public frmRmrb()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            
        }

        public override void InitializeData()
        {
            trvRptRoot.Nodes.Clear();
            lstRpt.Items.Clear();

            TrxNo = (int)ApplicationObject.FrmModuleList.SelectedNode["Trx No"];
            MainTableName = ApplicationObject.FrmModuleList.SelectedNode["Main Table Name"].ToString();
            dtRmrp1 = ApplicationService.QueryHelper.GetADataTable("SELECT * FROM rmrp1 WHERE NodeTrxNo = @s", TrxNo);

            var drNodes = from row in dtRmrp1.AsEnumerable()
                          where row["ParentTrxNo"].ToString() == ""
                          select row;

            foreach (DataRow drNode in drNodes)
            {
                TreeNode trn = new TreeNode();
                trn.Tag = drNode["TrxNo"];
                trn.Name = drNode["ReportName"].ToString();
                trn.Text = drNode["ReportCaption"].ToString();
                trvRptRoot.Nodes.Add(trn);
            }
        }

        public void InitReport(int ParentTrxNo)
        {
            lstRpt.Items.Clear();
            var drReports = from row in dtRmrp1.AsEnumerable()
                            where (int)(row["ParentTrxNo"].ToString() == "" ? 0 : row["ParentTrxNo"]) == ParentTrxNo
                            select row;

            foreach (DataRow drRpt in drReports)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Name = drRpt["ReportName"].ToString();
                lvi.Text = drRpt["ReportCaption"].ToString();
                lstRpt.Items.Add(lvi);
            }
        }

        private void trvRptRoot_AfterSelect(object sender, TreeViewEventArgs e)
        {
            InitReport((int)e.Node.Tag);
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            if (lstRpt.SelectedItems.Count > 0)
            {
                string ReportName = lstRpt.SelectedItems[0].Name;
                frmRmrp frmRptPreview = new frmRmrp(ReportName);
                frmRptPreview.Show();
            }
        }
    }
}